package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.BonusOfferDTO;
import static memberrewards.helpers.Constants.BLANK;

public class EnrollOffer extends ServletPojoAdapter {

	private static final long serialVersionUID = -1072100235042404392L;

	protected Map<String,String> getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan =params.get("pan")!=null? params.get("pan")[0]:null;
		final String itemId =params.get("bonusid")!=null? params.get("bonusid")[0]:null;
		
		//Default response
		final Map<String,String> ret = new HashMap<String, String>();
		ret.put("status", "Card Not Found");		
		ret.put("bonusid", "0");


		if (itemId == null || BLANK.equals(itemId)) {
			//Rubbish query
			ret.put("status", "Bonus information is not available");
		} else {
			final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);
			if (accountDetail!=null) {
				final List<BonusOfferDTO> items = RewardsDelegate.getEligibleBonusOffers(accountDetail.getTier());

				if(items == null || items.isEmpty()){
					// no items
					ret.put("status", "Not Eligible" );
				}else{
					//if we don't find it
					ret.put("status", "Bonus offer not found" );
					for (final BonusOfferDTO item : items) {					
						if(item.getOfferId().equals(itemId)){
							//found it, no need to continue to look
							ret.put("status", "Success" );
							ret.put("bonusid", itemId);
							break;
						}
					}
				}
			}		
		}
		return ret;
	}
}
